/**
 * @file lv_watch_slider.c
 *
 */

/*********************
 *      INCLUDES
 *********************/
#include "lv_watch.h"

#if USE_LV_WATCH_SLIDER != 0

#include <stdio.h>

/*********************
 *      DEFINES
 *********************/

/**********************
 *      TYPEDEFS
 **********************/
typedef struct {
    lv_cont_ext_t old_ext;
    uint8_t slide_type;
    lv_obj_t * slider;
} lv_watch_slide_obj_ext_t;

/**********************
 *  STATIC PROTOTYPES
 **********************/
static lv_res_t slider_obj_signal_func(lv_obj_t * obj, lv_signal_t sign, void * param);

/**********************
 *  STATIC VARIABLES
 **********************/

/**********************
 *      MACROS
 **********************/

/**********************
 *   GLOBAL FUNCTIONS
***********************/
static void slider_perpare_destroy(lv_obj_t * activity_obj)
{
    printf("slider destory\n");

    lv_obj_t * obj = lv_obj_get_child_back(activity_obj, NULL);
    while(obj) {
        if(lv_obj_get_signal_cb(obj) != slider_obj_signal_func) obj = lv_obj_get_child_back(activity_obj, obj);
        else break;
    }

    lv_watch_slide_obj_ext_t * ext = lv_obj_get_ext_attr(obj);
    if(ext->slide_type == SETTING_BRIGHTNESS) {
        setting_set_backlight_intensity(setting_get_backlight_intensity(), true);
    } else if(ext->slide_type == SETTING_VOLUME) {
        setting_set_ring_volume(setting_get_ring_volume(), true, false);
    } else if(ext->slide_type == SETTING_CALL_SPEAKER) {
        uint8_t volume = phone_get_call_volume();
        printf("%s: set nv, call speaker volume is %d.\n", __FUNCTION__, volume);
        UI_NV_Write_Req(NV_SECTION_UI_PHONE, 0, 1, &volume);
    } else if(ext->slide_type == SETTING_VOIP_SPEAKER) {
        uint8_t volume = voip_get_call_volume();
        printf("%s: set nv, voip speaker volume is %d.\n", __FUNCTION__, volume);
        UI_NV_Write_Req(NV_SECTION_UI_PHONE, 0, 1, &volume); //same nvm with phone
    }
}

lv_obj_t * watch_slider_get_slider(void)
{
    lv_obj_t * watch_obj = NULL;
    lv_watch_slide_obj_ext_t  * ext = NULL;
    lv_obj_t * activity_obj = lv_watch_get_activity_obj(ACT_ID_SLIDER);
    if(activity_obj) {
        lv_watch_get_child_obj(activity_obj, slider_obj_signal_func, &watch_obj);
    }

    if(watch_obj) {
        ext = lv_obj_get_ext_attr(watch_obj);
    }

    if(ext) return ext->slider;

    return NULL;
}

lv_obj_t * slider_create(lv_obj_t * activity_obj, setting_type_t setting_type, lv_coord_t ver_offset, lv_event_cb_t slider_event_cb)
{
    lv_obj_t * slider_activit_obj = lv_watch_get_activity_obj(ACT_ID_SLIDER);
    if(slider_activit_obj) return watch_slider_get_slider();

    if(activity_obj == NULL) {
        lv_watch_activity_ext_t activity_ext;
        memset(&activity_ext, 0, sizeof(lv_watch_activity_ext_t));
        activity_ext.actId = ACT_ID_SLIDER;
        activity_ext.create = NULL;
        activity_ext.prepare_destory = slider_perpare_destroy;
        activity_obj = lv_watch_creat_activity_obj(&activity_ext);
        LV_ASSERT_MEM(activity_obj);
        if(activity_obj == NULL) return NULL;
        lv_obj_add_style(activity_obj, LV_OBJ_PART_MAIN, &lv_style_transp);
        lv_obj_set_click(activity_obj, false);
    }

    lv_obj_t * obj = lv_obj_create(activity_obj, NULL);
    lv_obj_set_size(obj, lv_obj_get_width(activity_obj), lv_obj_get_height(activity_obj));
    lv_obj_add_style(obj, LV_OBJ_PART_MAIN, &lv_style_transp);

    lv_watch_set_free_ptr(obj, lv_obj_get_signal_cb(obj));
    lv_obj_set_signal_cb(obj, slider_obj_signal_func);
    lv_obj_set_click(obj, true);

    lv_obj_t * content = lv_cont_create(obj, NULL);
    lv_watch_slide_obj_ext_t * ext = lv_obj_allocate_ext_attr(obj, sizeof(lv_watch_slide_obj_ext_t));
    ext->slide_type = setting_type;

    LV_ASSERT_MEM(content);
    if(content == NULL) return NULL;
    lv_obj_add_style(content, LV_CONT_PART_MAIN, &lv_watch_cont_opa2);
    lv_obj_set_size(content, 204, 44);
    lv_obj_align(content, activity_obj, LV_ALIGN_IN_TOP_MID, 0, ver_offset);
    lv_cont_set_layout(content, LV_LAYOUT_OFF);
    lv_obj_set_click(content, true);

    lv_obj_t * slider = lv_slider_create(content, NULL);
    ext->slider = slider;
    lv_obj_set_size(slider, 186, 24);
    lv_obj_align(slider, content, LV_ALIGN_CENTER, 0, 0);

    if(setting_type == SETTING_CALL_SPEAKER) {
        lv_slider_set_range(slider, 0, (int16_t)HAL_AUDIO_SPK_LEVEL_MAX); /*refer to HAL_SPEAKER_GAIN */
        lv_slider_set_value(slider, phone_get_call_speaker_volume(), LV_ANIM_OFF);
    } else if(setting_type == SETTING_VOIP_SPEAKER) {
        lv_slider_set_range(slider, 0, (int16_t)HAL_AUDIO_SPK_LEVEL_MAX); /*refer to HAL_SPEAKER_GAIN */
        lv_slider_set_value(slider, phone_get_call_speaker_volume(), LV_ANIM_OFF);     /*same nvm with phone */
    } else if(setting_type == SETTING_VOLUME) {
        lv_slider_set_range(slider, 0, 10); /*refer to setting_volume.c*/
        lv_slider_set_value(slider, setting_get_ring_volume(), LV_ANIM_OFF);
    } else if(SETTING_LCD_COLOR == setting_type) {
        lv_slider_set_range(slider, 0, 10);
        lv_slider_set_value(slider, autotest_aging_lcdtest_get_brightness(), LV_ANIM_OFF);
#if USE_LV_BLUETOOTH != 0
    } else if(SETTING_BT_VOLUME == setting_type) {
        lv_slider_set_range(slider, 0, HAL_BT_VOLUME_MAX);
#endif
    } else {
        lv_slider_set_range(slider, 0, 10); /*refer to setting_brightness.c*/
        lv_slider_set_value(slider, setting_get_backlight_intensity(), LV_ANIM_OFF);
    }

    lv_obj_set_style_local_bg_color(slider, LV_SLIDER_PART_BG, LV_STATE_DEFAULT, WATCH_COLOR_GRAY);
    lv_obj_set_style_local_bg_grad_color(slider, LV_SLIDER_PART_BG, LV_STATE_DEFAULT, LV_COLOR_SILVER);
    lv_obj_set_style_local_bg_opa(slider, LV_SLIDER_PART_BG, LV_STATE_DEFAULT, LV_OPA_30);
    lv_obj_set_style_local_border_color(slider, LV_SLIDER_PART_BG, LV_STATE_DEFAULT, LV_COLOR_MAKE(0x40, 0x40, 0x40));
    lv_obj_set_style_local_border_opa(slider, LV_SLIDER_PART_BG, LV_STATE_DEFAULT, LV_OPA_30);
    lv_obj_set_style_local_radius(slider, LV_SLIDER_PART_BG, LV_STATE_DEFAULT, LV_RADIUS_CIRCLE);
    lv_obj_set_style_local_line_color(slider, LV_SLIDER_PART_BG, LV_STATE_DEFAULT, LV_COLOR_MAKE(0x20, 0x20, 0x20));
    lv_obj_set_style_local_pad_left(slider, LV_SLIDER_PART_BG, LV_STATE_DEFAULT, 15);
    lv_obj_set_style_local_pad_right(slider, LV_SLIDER_PART_BG, LV_STATE_DEFAULT, 15);
    lv_obj_set_style_local_pad_top(slider, LV_SLIDER_PART_BG, LV_STATE_DEFAULT, 15);
    lv_obj_set_style_local_pad_bottom(slider, LV_SLIDER_PART_BG, LV_STATE_DEFAULT, 15);

    lv_color_t slide_color;
    if(setting_type == SETTING_BRIGHTNESS) {
        slide_color = WATCH_COLOR_ORANGE;
    } else {
        slide_color = WATCH_COLOR_BLUE;
    }

    lv_obj_set_style_local_bg_color(slider, LV_SLIDER_PART_INDIC, LV_STATE_DEFAULT, slide_color);
    lv_obj_set_style_local_bg_grad_color(slider, LV_SLIDER_PART_INDIC, LV_STATE_DEFAULT, slide_color);
    lv_obj_set_style_local_border_width(slider, LV_SLIDER_PART_INDIC, LV_STATE_DEFAULT, 0);
    lv_obj_set_style_local_shadow_width(slider, LV_SLIDER_PART_INDIC, LV_STATE_DEFAULT, 0);

    lv_obj_set_style_local_radius(slider, LV_SLIDER_PART_KNOB, LV_STATE_DEFAULT, LV_RADIUS_CIRCLE);
    lv_obj_set_style_local_bg_color(slider, LV_SLIDER_PART_KNOB, LV_STATE_DEFAULT, slide_color);
    lv_obj_set_style_local_bg_grad_color(slider, LV_SLIDER_PART_KNOB, LV_STATE_DEFAULT, slide_color);
    lv_obj_set_style_local_shadow_color(slider, LV_SLIDER_PART_KNOB, LV_STATE_DEFAULT, LV_COLOR_WHITE);
    lv_obj_set_style_local_shadow_width(slider, LV_SLIDER_PART_KNOB, LV_STATE_DEFAULT, 5);
    lv_obj_set_style_local_border_width(slider, LV_SLIDER_PART_KNOB, LV_STATE_DEFAULT, 2);
    lv_obj_set_style_local_border_opa(slider, LV_SLIDER_PART_KNOB, LV_STATE_DEFAULT, LV_OPA_50);
    lv_obj_set_style_local_pad_top(slider, LV_SLIDER_PART_KNOB, LV_STATE_DEFAULT, 0);
    lv_obj_set_style_local_pad_bottom(slider, LV_SLIDER_PART_KNOB, LV_STATE_DEFAULT, 0);
    lv_obj_set_style_local_pad_left(slider, LV_SLIDER_PART_KNOB, LV_STATE_DEFAULT, 0);
    lv_obj_set_style_local_pad_right(slider, LV_SLIDER_PART_KNOB, LV_STATE_DEFAULT, 0);

    lv_obj_set_event_cb(slider, slider_event_cb);

    lv_anim_path_t path;
    lv_anim_path_init(&path);
    lv_anim_path_set_cb(&path, lv_anim_path_linear);
    lv_anim_t a;
    lv_anim_init(&a);
    lv_anim_set_var(&a, content);
    lv_anim_set_values(&a, -lv_obj_get_height(content), lv_obj_get_y(content));
    lv_anim_set_exec_cb(&a, (lv_anim_exec_xcb_t)lv_obj_set_y);
    lv_anim_set_time(&a, 200);
    lv_anim_set_delay(&a, 0);
    lv_anim_set_path(&a, &path);
    lv_anim_start(&a);
    // lv_obj_animate(content, LV_ANIM_FLOAT_TOP | LV_ANIM_IN, 200, 0, NULL);

    return slider;
}

/**********************
 *   STATIC FUNCTIONS
 **********************/
static void slider_obj_hide_anim_end(lv_anim_t * a)
{
    lv_obj_t * obj = a->var;
    lv_watch_slide_obj_ext_t * ext = lv_obj_get_ext_attr(obj);
    if(ext->slide_type == SETTING_BRIGHTNESS) {
        setting_set_backlight_intensity(setting_get_backlight_intensity(), true);
    } else if(ext->slide_type == SETTING_VOLUME) {
        setting_set_ring_volume(setting_get_ring_volume(), true, false);
    } else if(ext->slide_type == SETTING_CALL_SPEAKER) {
        uint8_t volume = phone_get_call_volume();
        printf("%s: set nv, call speaker volume is %d.\n", __FUNCTION__, volume);
        UI_NV_Write_Req(NV_SECTION_UI_PHONE, 0, 1, &volume);
    } else if(ext->slide_type == SETTING_VOIP_SPEAKER) {
        uint8_t volume = voip_get_call_volume();
        printf("%s: set nv, voip speaker volume is %d.\n", __FUNCTION__, volume);
        UI_NV_Write_Req(NV_SECTION_UI_PHONE, 0, 1, &volume);
    }

    if(obj) {
        lv_obj_t * activity_obj = lv_obj_get_parent(obj);
        if(activity_obj) {
            lv_obj_del(activity_obj);
        } else {
            lv_obj_del(obj);
        }
    }
}

static lv_res_t slider_obj_signal_func(lv_obj_t * obj, lv_signal_t sign, void * param)
{
    lv_res_t res;

    if(sign == LV_SIGNAL_RELEASED) {
        if(obj) {
            lv_anim_path_t path;
            lv_anim_path_init(&path);
            lv_anim_path_set_cb(&path, lv_anim_path_linear);
            lv_anim_t a;
            lv_anim_init(&a);
            lv_anim_set_var(&a, obj);
            lv_anim_set_values(&a,  lv_obj_get_y(obj), -lv_obj_get_height(obj));//LV_ANIM_OUT opposite start&end
            lv_anim_set_exec_cb(&a, (lv_anim_exec_xcb_t)lv_obj_set_y);
            lv_anim_set_ready_cb(&a,(lv_anim_ready_cb_t)slider_obj_hide_anim_end);
            lv_anim_set_time(&a, 200);
            lv_anim_set_delay(&a, 0);
            lv_anim_set_path(&a, &path);
            lv_anim_start(&a);
            //lv_obj_animate(obj, LV_ANIM_FLOAT_TOP | LV_ANIM_OUT, 200, 0, slider_obj_hide_anim_end);
        }
    }

    /* Include the ancient signal function */
    lv_signal_cb_t sign_func = (lv_signal_cb_t)lv_obj_get_user_data(obj).user_data;
    res = sign_func(obj, sign, param);
    if(res != LV_RES_OK) return res;

    return  LV_RES_OK;
}


#endif /*USE_LV_WATCH_SLIDER*/
